package com.xjrsoft.module.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.module.system.dto.ErpConfigDto;
import com.xjrsoft.module.system.entity.ErpConfig;
import com.xjrsoft.module.system.model.ErpConfigModel;
import com.xjrsoft.module.system.service.IErpConfigService;
import com.xkcoding.http.util.StringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;

/**
 * 系统配置
 * @Author: lxl
 * @Date: 2024/6/25 14:13
 */
@RestController
@RequestMapping(GlobalConstant.SYSTEM_MODULE_PREFIX + "/erp-config")
@Api(value = GlobalConstant.SYSTEM_MODULE_PREFIX + "/erp-config", tags = "Erp对接配置")
@AllArgsConstructor
public class ErpConfigController {

    private final IErpConfigService erpConfigService;

    @GetMapping("/list")
    @ApiOperation("查询所有系统配置")
    public R list() {
        List<ErpConfig> list = erpConfigService.list();
        return R.ok(list);
    }

    @GetMapping("/info")
    @ApiOperation("查询某一个配置")
    public R info(@RequestParam Long id){
        return R.ok(erpConfigService.getById(id));
    }

    @GetMapping("/info-by-type")
    @ApiOperation("根据类型查询某一个配置")
    public R infoByType(@RequestParam Integer type){
        ErpConfig one = erpConfigService.getOne(Wrappers.lambdaQuery(ErpConfig.class).eq(ErpConfig::getErpType, type));

        return R.ok(one);
    }


    @PutMapping
    @ApiOperation("修改某个配置")
    public  R update(@Valid @RequestBody ErpConfigDto dto){
        return R.ok(erpConfigService.updateErpConfig(dto));
    }
    @GetMapping(value = "/k3")
    @ApiOperation(value = "金蝶K3-demo信息")
    public R k3(@RequestParam String param) throws Exception {
       if (StringUtil.isEmpty(param)) 
       {
         param="{\"FormId\":\"SAL_SaleOrder\",\"FieldKeys\":\"FBillNo,FQty\",\"FilterString\":[],\"OrderString\":\"\",\"TopRowCount\":0,\"StartRow\":0,\"Limit\":2000,\"SubSystemId\":\"\"}";
       }
        String resultJson=erpConfigService.execErp(param);
        return R.ok(resultJson);
    }
}
